<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: #f00;
            visibility: visible;
        }
    </style>
</head>
<body>

    <input id="inp" type="password">
    <button id="btn">显示</button>

    <script>

        var inp = document.querySelector('#inp');
        var btn = document.querySelector('#btn');
        var isHide = true;

        btn.addEventListener('click', onClick);

        function onClick () {
            // if (isHide) {
            //     // 点击时要做的事
            //     btn.innerText = '隐藏';
            //     inp.setAttribute('type', 'text');
            // } else {
            //     btn.innerText = '显示';
            //     inp.setAttribute('type', 'password');
            // }
            btn.innerText = isHide ? '隐藏' : '显示';
            inp.setAttribute('type', isHide ? 'text' : 'password');
            isHide = !isHide;
        }


    </script>
</body>
</html>